//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nc.report.widget.action;

import com.google.gson.Gson;
import com.ufida.report.anareport.areaset.ExtendAreaSetUtil;
import com.ufida.report.free.userdef.FormSchemeParam;
import java.util.HashMap;
import java.util.Map;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.itf.iufo.freereport.IFreeReportQryService;
import nc.itf.report.frreport.IFreeReportService;
import nc.itf.report.frreport.IReportInterUitl;
import nc.service.report.wiget.ReportQueryVO;
import nc.ui.querytemplate.querytree.IQueryScheme;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.platform.workbench.AppRegisterVO;
import nc.vo.pub.lang.ICalendar;
import nc.vo.pub.lang.UFDouble;
import nccloud.dto.baseapp.querytree.dataformat.QueryTreeFormatVO;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.core.json.IJson;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.container.SessionContext;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.querytemplet.QueryUtil4NCC;
import nccloud.pubitf.platform.workbench.IAppRegister;
import nccloud.web.lightbq.json.LappJsonBuilder;
import nccloud.web.lightbq.json.LappJsonFactory;
import org.apache.commons.lang3.StringUtils;
import uap.json.JSONObject;

public class LightReportQueryDataAction implements ICommonAction {
    public LightReportQueryDataAction() {
    }

    public Object doAction(IRequest request) {
        boolean isMobileAction = request.isMobileAction();
        if (isMobileAction) {
            InvocationInfoProxy.getInstance().setProperty("isMobileReportQuery", String.valueOf(true));
        }

        String jsonstr = request.read();
        if (jsonstr == null || jsonstr.trim().isEmpty()) {
            String message = NCLangRes4VoTransl.getNCLangRes().getStrByID("100303_0", "0100303-0102");
            ExceptionUtils.wrapBusinessException(message);
        }

        IJson gson = JsonFactory.create();
        QueryTreeFormatVO queryTreeVO = (QueryTreeFormatVO)gson.fromJson(jsonstr, QueryTreeFormatVO.class);
        Map<String, Object> jsonMap = (Map)gson.fromJson(jsonstr, Map.class);
        Boolean controlByLicense = (Boolean)jsonMap.get("controlByLicense");
        if (controlByLicense != null && controlByLicense) {
            try {
                boolean lessThanLicense = ((IFreeReportQryService)ServiceLocator.find(IFreeReportQryService.class)).isReportCountLessThanLicense(true, "14130RPTM01");
//                if (!lessThanLicense) { // 取消报表数量限制
//                    ExceptionUtils.wrapBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("100303_0", "0100303-0125"));
//                }
            } catch (Exception var28) {
                Exception ex = var28;
                ExceptionUtils.wrapException(ex);
            }
        }

        IReportInterUitl reportInterUtil = (IReportInterUitl)ServiceLocator.find(IReportInterUitl.class);
        String reportId = (String)jsonMap.get("pk_report");
        String appCode = (String)jsonMap.get("appcode");
        String whereSql = "code = '" + appCode + "'";
        AppRegisterVO[] appRegisterVO = ((IAppRegister)ServiceLocator.find(IAppRegister.class)).queryAppRegisterVOs(whereSql, true);
        String module = appRegisterVO[0].getOwn_module();
        if ("1031".equals(module)) {
            InvocationInfoProxy.getInstance().setProperty("isMobileReportQuery", String.valueOf(true));
        }

        String opents = (String)jsonMap.get("opents");
        String userId = SessionContext.getInstance().getClientInfo().getUserid();
        String groupId = SessionContext.getInstance().getClientInfo().getPk_group();
        String pageSize;
        if (queryTreeVO.getPageInfo() != null && queryTreeVO.getPageInfo().getPageSize() != null) {
            pageSize = queryTreeVO.getPageInfo().getPageSize();
        } else {
            pageSize = String.valueOf(reportInterUtil.call_sysparam("BQ0016"));
        }

        boolean isAdvanced = (Boolean)jsonMap.get("isAdvanced");
        int[] timeOffsetArr = this.getOffsetInt(jsonMap);
        int timeZoneOffset = timeOffsetArr[0];
        int resTimeZoneOffset = timeOffsetArr[1];
        String reqTotalData;
        if (queryTreeVO.getUserdefObj() != null && queryTreeVO.getUserdefObj().get("key_formScheme") != null) {
            IJson gsonForm = LappJsonFactory.create();
            Gson gsonRes = LappJsonBuilder.create();
            reqTotalData = gsonRes.toJson(queryTreeVO.getUserdefObj().get("key_formScheme"));
            FormSchemeParam formSchemeParam = (FormSchemeParam)gsonForm.fromJson(reqTotalData, FormSchemeParam.class);
            FormSchemeParam formSchemeParamClone = (FormSchemeParam)gsonForm.fromJson(reqTotalData, FormSchemeParam.class);
            ExtendAreaSetUtil.getRealContentSet(formSchemeParam);
            ExtendAreaSetUtil.getRealContentSet(formSchemeParamClone);
            queryTreeVO.getUserdefObj().put("key_formScheme_clone", formSchemeParamClone);
            queryTreeVO.getUserdefObj().put("key_formScheme", formSchemeParam);
        }

        ReportQueryVO reportQueryVO = new ReportQueryVO();
        reportQueryVO.initForQuery(reportId, appCode, queryTreeVO, opents, userId, groupId, isAdvanced, timeZoneOffset);
        reportQueryVO.setResTimeZoneOffset(resTimeZoneOffset);
        reportQueryVO.setPageSize(pageSize);
        Map<String, Object> paramMap = new HashMap();
        reqTotalData = (String)jsonMap.get("requireTotalData");
        if (Boolean.parseBoolean(reqTotalData)) {
            paramMap.put("requireTotalData", true);
        }

        if (!StringUtils.isEmpty(queryTreeVO.getOid())) {
            QueryUtil4NCC queryUtil = new QueryUtil4NCC(queryTreeVO);
            IQueryScheme queryScheme = queryUtil.convertCondition();
            paramMap.put("queryScheme", queryScheme);
        }

        Boolean isSmartBASearch = (Boolean)jsonMap.get("smartba_search");
        paramMap.put("smartba_search", isSmartBASearch);
        Boolean treeType = (Boolean)jsonMap.get("treeType");
        paramMap.put("treeType", treeType);
        reportQueryVO.setParamMap(paramMap);
        this.onlyShowDetailOrCount(jsonMap, reportQueryVO);
        JSONObject reportDataJson = ((IFreeReportService)ServiceLocator.find(IFreeReportService.class)).baseQueryWebReport(reportQueryVO);
        InvocationInfoProxy.getInstance().setProperty("isMobileReportQuery", (String)null);
        return reportDataJson;
    }

    protected int[] getOffsetInt(Map jsonMap) {
        Object offset = jsonMap.get("key_time_offset");
        UFDouble offsetUFDouble;
        Double accTimeZone;
        double zoneUfDouble;
        if (offset == null) {
            offsetUFDouble = new UFDouble(0);
            accTimeZone = 0.0;
        } else {
            if (offset instanceof String) {
                zoneUfDouble = Double.parseDouble((String)offset);
                accTimeZone = (zoneUfDouble - (double)(ICalendar.BASE_TIMEZONE.getRawOffset() / '\uea60')) / 60.0;
            } else {
                accTimeZone = ((Double)offset - (double)(ICalendar.BASE_TIMEZONE.getRawOffset() / '\uea60')) / 60.0;
            }

            offsetUFDouble = InvocationInfoProxy.getInstance().getSensitiveTimeZone() ? new UFDouble(accTimeZone) : new UFDouble(0);
        }

        zoneUfDouble = Math.ceil(offsetUFDouble.toDouble());
        return new int[]{(int)zoneUfDouble, (new UFDouble(accTimeZone)).intValue()};
    }

    protected void onlyShowDetailOrCount(Map<String, Object> jsonMap, ReportQueryVO reportQueryVO) {
    }
}
